package main

import (
	"end/service/activity"
	"end/service/admin"
	"end/service/business"
	"end/service/clerk"
	"end/service/commodity"
	"end/service/eventRegistration"
	"end/service/file"
	"end/service/order"
	"end/service/user"
	"end/service/wxutils"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 设置允许跨域访问的域名为任意域
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")

		// 设置允许的方法和头
		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

		// 如果是OPTIONS请求，直接返回成功，不再执行后续处理器
		if c.Request.Method == http.MethodOptions {
			c.AbortWithStatus(http.StatusNoContent)
			return
		}

		// 继续执行下一个处理器函数
		c.Next()
	}
}

func main() {
	r := gin.Default()

	r.Use(CORSMiddleware())

	user.IsLogin(r)
	user.Register(r)
	user.GetMeInfo(r)
	user.AllOrder(r)
	user.Login(r)
	user.AllUser(r)
	user.UpdateUser(r)
	user.GetUserStatus(r)
	user.DelMeBusiness(r)
	user.SetIntegral(r)

	admin.AddAdmin(r)
	admin.UpdateAdmin(r)
	admin.IsAdmin(r)
	admin.DelAdmin(r)
	admin.AllAdmin(r)
	admin.InfoALlAdmin(r)
	admin.AllBusiness(r)

	activity.AuditActivity(r)
	activity.CreateActivity(r)
	activity.DeleteActivity(r)
	activity.ShowActivityDetail(r)
	activity.GetEventsAudit(r)
	activity.ShowEvents(r)
	activity.CreateAllActivity(r)
	activity.CreateActivityScan(r)

	business.IsBusiness(r)
	business.AddBusiness(r)
	business.AuditBusiness(r)
	business.AllBusiness(r)
	business.DelBusiness(r)
	business.UpdateBusiness(r)
	business.InfoBusiness(r)

	clerk.IsClerk(r)
	clerk.AddClerk(r)
	clerk.AllClerk(r)
	clerk.DelClerk(r)
	clerk.GetBusiness(r)

	commodity.UpdateCommodity(r)
	commodity.AllCommodity(r)
	commodity.DetailsCommodity(r)
	commodity.DelCommodity(r)
	commodity.UploadCommodity(r)
	commodity.ShopCommodity(r)
	commodity.StatusCommodity(r)

	order.ConfirmOrder(r)
	order.GetQrCode(r)
	order.BusinessAllCommodity(r)
	order.GetAllOrder(r)

	eventRegistration.EventAll(r)

	wxutils.GetAccessToken(r)
	wxutils.GetOpenid(r)
	wxutils.GetPhoneNumber(r)

	file.UploadFile(r)

	err := r.Run(":8080")
	if err != nil {
		return
	}
}
